//
//  HomeDropButton.m
//  TengyunStation
//
//  Created by Johnny on 15/11/13.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "HomeDropButton.h"

#define BgImageWidth        133.0
#define BgImageHeight       111.0

@interface HomeCellButton : UIButton {
    UIImageView *_imageView;
    UILabel *_titleLabel;
}

@end

@implementation HomeCellButton

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        float y = (frame.size.height - 22)/2.0;
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, y, 22, 22)];
        [self addSubview:_imageView];
        float width = frame.size.width - 40 - 12;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, y, width, 22)];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:_titleLabel];
    }
    return self;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state {
    _imageView.image = image;
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    _titleLabel.text = title;
}

@end

@implementation HomeDropButton

- (id)initWithDelegate:(id<HomeDropButtonDelegate>)delegate InView:(UIView *)view {
    self = [super initWithFrame:CGRectMake(0, 0, 22, 22)];
    if (self) {
        _delegate = delegate;
        _superView = view;
        _bgView = [[UIView alloc] initWithFrame:view.frame];
        _bgView.backgroundColor = [UIColor clearColor];
        _bgView.alpha = 0;
        [view addSubview:_bgView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView:)];
        tap.numberOfTapsRequired = 1;
        tap.delegate = self;
        [_bgView addGestureRecognizer:tap];
        [self reloadImageView];
        [self setImage:[UIImage imageNamed:@"home_btn_add"] forState:UIControlStateNormal];
        [self addTarget:self action:@selector(selfButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)reloadImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, BgImageWidth, BgImageHeight)];
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"home_bg_add"];
    [_bgView addSubview:imageView];
    float height = (BgImageHeight - 10)/2.0;
    for (int i = 0; i < 2; i ++) {
        HomeCellButton *button = [[HomeCellButton alloc] initWithFrame:CGRectMake(0, 10 + height * i, BgImageWidth, height)];
        if (i == 0) {
            [button setImage:[UIImage imageNamed:@"home_icon_zitw"] forState:UIControlStateNormal];
            [button setTitle:@"自提件投递" forState:UIControlStateNormal];
        } else {
            [button setImage:[UIImage imageNamed:@"home_icon_shangmw"] forState:UIControlStateNormal];
            [button setTitle:@"上门件投递" forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10 + i;
        [imageView addSubview:button];
    }
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(8, 10 + height, BgImageWidth - 20, 0.5)];
    line.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
    [imageView addSubview:line];
}

- (void)buttonAction:(UIButton *)button {
    NSInteger tag = button.tag - 10;
    [self bgViewHide];
    if ([_delegate respondsToSelector:@selector(didFinishHomeDropButtonTag:)]) {
        [_delegate didFinishHomeDropButtonTag:tag];
    }
}

#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:_bgView];
    if (location.x <= (BgImageWidth + 5) && location.y <= (BgImageHeight + 5)) {
        return NO;
    }
    return YES;
}
//点击手势
- (void)hideBgView:(UITapGestureRecognizer *)recognizer {
    [self bgViewHide];
}

- (void)selfButtonAction {
    if (_isShow) {
        [self bgViewHide];
    } else {
        [self bgViewShow];
    }
}
//显示
- (void)bgViewShow {
    _isShow = YES;
    [_superView addSubview:_bgView];
    [UIView animateWithDuration:0.1 animations:^{
        _bgView.alpha = 1;
    }];
}
//隐藏
- (void)bgViewHide {
    _isShow = NO;
    [UIView animateWithDuration:0.1 animations:^{
        _bgView.alpha = 0;
    } completion:^(BOOL finished) {
        [_bgView removeFromSuperview];
    }];
}

@end
